package com.yanqu.road.dao.impl.activity.sonhai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiBeastData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiBeastGroupData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiPatronsData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiPlotData;
import com.yanqu.road.utils.string.StringUtils;
import org.checkerframework.checker.units.qual.C;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SonHaiPlotDataDaoImpl extends TempDataObjectDao<SonHaiPlotData> {

    public static String getSonHaiPlotDataSql = "select * from `t_c_activity_son_hai_plot_data` where `activityId` = ? and groupId = ? and delFlag = 0";
    public Map<Integer, Map<Integer, Map<String, SonHaiPlotData>>> getSonHaiPlotDataMap(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        //K:队伍ID,K:区域ID,K:地块坐标x_y
        Map<Integer, Map<Integer, Map<String, SonHaiPlotData>>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSonHaiPlotDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                SonHaiPlotData data = parse(rs);
                Map<Integer, Map<String, SonHaiPlotData>> regionMap = map.computeIfAbsent(data.getTeamId(), k -> new ConcurrentHashMap<>());
                Map<String, SonHaiPlotData> plotMap = regionMap.computeIfAbsent(data.getRegionId(), k -> new ConcurrentHashMap<>());
                plotMap.put(data.getX() + "_" + data.getY(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateSonHaiPlotDataSql = "update `t_c_activity_son_hai_plot_data` set userId=?,plotId=?,type=?,level=?,winFlag=?,winTime=?,getRewardFlag=?,reward=?,extraAuraScore=?,beastData=?,pvpUserId=?,eggId=?,delFlag=?,attNumMap=?,chooseRewardFlag=?,createTime=? where activityId=? and groupId = ? and teamId = ? and regionId = ? and regionUniqueId = ? and x = ? and y = ?";
    @Override
    protected boolean updateData(SonHaiPlotData sonHaiPlotData) {
        sonHaiPlotData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiPlotData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getPlotId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getWinFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiPlotData.getWinTime()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getGetRewardFlag()));
        para.put(i++, new DbParameter(Types.VARCHAR, sonHaiPlotData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiPlotData.getExtraAuraScore()));
        String beastGroupData = "";
        if (sonHaiPlotData.getBeastData() != null) {
            beastGroupData = JSON.toJSONString(sonHaiPlotData.getBeastData());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, beastGroupData));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiPlotData.getPvpUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getEggId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getDelFlag()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiPlotData.getAttNumMap())));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getChooseRewardFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiPlotData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getRegionId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getRegionUniqueId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getX()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getY()));
        if (getDb().executeNoneQuery(updateSonHaiPlotDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSonHaiPlotDataSql = "insert into `t_c_activity_son_hai_plot_data`(activityId,groupId,teamId,userId,regionUniqueId,regionId,x,y,plotId,type,level,winFlag,winTime,getRewardFlag,reward,extraAuraScore,beastData,pvpUserId,eggId,delFlag,attNumMap,chooseRewardFlag,createTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SonHaiPlotData sonHaiPlotData) {
        sonHaiPlotData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiPlotData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getRegionUniqueId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getRegionId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getX()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getY()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getPlotId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getWinFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiPlotData.getWinTime()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getGetRewardFlag()));
        para.put(i++, new DbParameter(Types.VARCHAR, sonHaiPlotData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiPlotData.getExtraAuraScore()));
        String beastGroupData = "";
        if (sonHaiPlotData.getBeastData() != null) {
            beastGroupData = JSON.toJSONString(sonHaiPlotData.getBeastData());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, beastGroupData));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiPlotData.getPvpUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getEggId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getDelFlag()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiPlotData.getAttNumMap())));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiPlotData.getChooseRewardFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiPlotData.getCreateTime()));
        if (getDb().executeNoneQuery(insertSonHaiPlotDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SonHaiPlotData parseData(ResultSet rs) throws SQLException {
        SonHaiPlotData sonHaiPlotData = new SonHaiPlotData();
        sonHaiPlotData.setActivityId(rs.getInt("activityId"));
        sonHaiPlotData.setGroupId(rs.getInt("groupId"));
        sonHaiPlotData.setTeamId(rs.getInt("teamId"));
        sonHaiPlotData.setUserId(rs.getLong("userId"));
        sonHaiPlotData.setRegionUniqueId(rs.getInt("regionUniqueId"));
        sonHaiPlotData.setRegionId(rs.getInt("regionId"));
        sonHaiPlotData.setX(rs.getInt("x"));
        sonHaiPlotData.setY(rs.getInt("y"));
        sonHaiPlotData.setPlotId(rs.getInt("plotId"));
        sonHaiPlotData.setType(rs.getInt("type"));
        sonHaiPlotData.setLevel(rs.getInt("level"));
        sonHaiPlotData.setWinFlag(rs.getInt("winFlag"));
        sonHaiPlotData.setWinTime(rs.getLong("winTime"));
        sonHaiPlotData.setGetRewardFlag(rs.getInt("getRewardFlag"));
        sonHaiPlotData.setReward(rs.getString("reward"));
        sonHaiPlotData.setExtraAuraScore(rs.getLong("extraAuraScore"));
        if (!StringUtils.isNullOrEmpty(rs.getString("beastData"))) {
            sonHaiPlotData.setBeastData(JSON.parseObject(rs.getString("beastData"), SonHaiBeastData.class));
        }
        sonHaiPlotData.setPvpUserId(rs.getLong("pvpUserId"));
        sonHaiPlotData.setEggId(rs.getInt("eggId"));
        if (!StringUtils.isNullOrEmpty(rs.getString("attNumMap"))) {
            sonHaiPlotData.setAttNumMap(JSON.parseObject(rs.getString("attNumMap"), new TypeReference<Map<Long, Integer>>() {}));
        }
        sonHaiPlotData.setCreateTime(rs.getLong("createTime"));
        sonHaiPlotData.setChooseRewardFlag(rs.getInt("chooseRewardFlag"));
        return sonHaiPlotData;
    }
}
